package com.briup.pts.service.impl;

import cn.hutool.core.util.StrUtil;
import com.briup.pts.entity.ManagerBatch;
import com.briup.pts.entity.QuarantineRegistration;
import com.briup.pts.entity.QuarantineRegistrationExample;
import com.briup.pts.exception.ServiceException;
import com.briup.pts.mapper.ManagerBatchMapper;
import com.briup.pts.mapper.QuarantineRegistrationMapper;
import com.briup.pts.result.ResultCode;
import com.briup.pts.service.QuarantineRegistrationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class QuarantineRegistrationServiceImpl implements QuarantineRegistrationService {

    @Autowired
    private QuarantineRegistrationMapper quarantineRegistrationMapper;
    @Autowired
    private ManagerBatchMapper managerBatchMapper;

    @Override
    public PageInfo<QuarantineRegistration> findAllByPage(String bQualified,
                                                          String grMechanism,
                                                          Integer pageNum,
                                                          Integer pageSize) {
        // 开启分页
        PageHelper.startPage(pageNum, pageSize);
        // 使用example查询全部, 不拼接任何where的条件
        QuarantineRegistrationExample quarantineRegistrationExample = new QuarantineRegistrationExample();
        // 添加拼接条件
        QuarantineRegistrationExample.Criteria criteria = quarantineRegistrationExample.createCriteria();
        if (!StrUtil.isEmpty(bQualified)) {
            criteria = criteria.andBQualifiedEqualTo(bQualified);
        }
        if (!StrUtil.isEmpty(grMechanism)) {
            criteria = criteria.andGrMechanismLike("%" + grMechanism + "%");
        }
        List<QuarantineRegistration> quarantineRegistrations = quarantineRegistrationMapper.selectByExample(quarantineRegistrationExample);
        // 返回pageInfo的对象
        return new PageInfo<>(quarantineRegistrations);


    }

    @Override
    @Transactional
    public void saveOrUpdate(QuarantineRegistration qr) {
        // 根据qr对象这种是否有id区分新增和更新
        Integer grId = qr.getGrId();
        if (grId == null){
            // 新增逻辑
            int insert = quarantineRegistrationMapper.insert(qr);

            if (insert == 1){
                // 新增成功之后要更新批次信息里的检疫状态和检疫结果
                // 需要通过主键的返回值来进行查询
                Integer pk = qr.getGrId();
                // 更新批次表
                ManagerBatch managerBatch = new ManagerBatch();
                // 更新检疫状态为 已检疫
                managerBatch.setBQuarantine("已检疫");
                // 检疫是否合格传进去
                managerBatch.setBQualified(qr.getBQualified());
                managerBatch.setBSerialId(qr.getGrBatchId());
                int r = managerBatchMapper.updateByPrimaryKeySelective(managerBatch);
                // 根据r的值判断是否更新成功了
                // ... 暂时不写，事务管理没加
                if (r != 1){
                    throw new ServiceException(ResultCode.FAIL);
                }
            } else {
                // 先定义成操作失败，后续可以在枚举里加【插入数据失败】
                throw new ServiceException(ResultCode.FAIL);
            }
        } else {
            // 更新逻辑
            quarantineRegistrationMapper.updateByPrimaryKeySelective(qr);
        }
    }

    @Override
    @Transactional
    public void removeById(Integer id) {
        QuarantineRegistration quarantineRegistration = quarantineRegistrationMapper.selectByPrimaryKey(id);
        String grBatchId = quarantineRegistration.getGrBatchId();
        ManagerBatch managerBatch = managerBatchMapper.selectByPrimaryKey(grBatchId);
        managerBatch.setBQuarantine("未检疫");
        int j = managerBatchMapper.updateByPrimaryKeySelective(managerBatch);
        if (j == 0){
            throw new ServiceException(ResultCode.FAIL + "删除检疫记录，修改批次为未检疫失败");
        }
        int i = quarantineRegistrationMapper.deleteByPrimaryKey(id);
        if (i == 0){
            throw new ServiceException(ResultCode.FAIL + "删除检疫记录失败");
        }
    }

    @Override
    @Transactional
    public void removeBatch(List<Integer> ids) {
        ids.forEach((id)->{
            QuarantineRegistration quarantineRegistration = quarantineRegistrationMapper.selectByPrimaryKey(id);
            String grBatchId = quarantineRegistration.getGrBatchId();
            ManagerBatch managerBatch = managerBatchMapper.selectByPrimaryKey(grBatchId);
            managerBatch.setBQuarantine("未检疫");
            int j = managerBatchMapper.updateByPrimaryKeySelective(managerBatch);
            if (j == 0){
                throw new ServiceException(ResultCode.FAIL + "批量删除检疫记录，修改批次为未检疫失败");
            }
        });

        // 使用了多次sql语句，不好
//        for (Integer id : ids) {
//            removeById(id);
//        }

        QuarantineRegistrationExample example = new QuarantineRegistrationExample();
        example.createCriteria().andGrIdIn(ids);
        int i = quarantineRegistrationMapper.deleteByExample(example);
        if (i == 0){
            throw new ServiceException(ResultCode.FAIL + "批量删除检疫记录失败");
        }
    }

}
